package com.swz.dcrm.ui.daily;


import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.DailyTask;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.AnalyzeViewModel;
import com.swz.dcrm.util.Tool;

import java.util.ArrayList;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class DailyTaskFragment extends BaseFragment {

    @BindView(R.id.tv_has_visit)
    TextView tvHasVisit;
    @BindView(R.id.tv_not_visit)
    TextView tvNotVisit;
    @BindView(R.id.tv_state1)
    TextView tvState1;
    @BindView(R.id.tv_state2)
    TextView tvState2;
    @BindView(R.id.tv_state3)
    TextView tvState3;
    @BindView(R.id.pie_chart1)
    PieChart pieChart1;
    @BindView(R.id.pie_chart2)
    PieChart pieChart2;

    @Inject
    AnalyzeViewModel analyzeViewModel;

    public static DailyTaskFragment newInstance() {

        return new DailyTaskFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        analyzeViewModel.getDailyTask().observe(this, new Observer<BaseResponse<DailyTask>>() {
            @Override
            public void onChanged(BaseResponse<DailyTask> dailyTaskBaseResponse) {
                if(dailyTaskBaseResponse!=null&&dailyTaskBaseResponse.isSuccess()){
                    DailyTask dailyTask=dailyTaskBaseResponse.getData();


                    String str =dailyTask.getFollowCount()+" ("+
                            dailyTask.getFollowRate()+"%)" ;

                    tvHasVisit.setText(getSpannableString(str,'(',R.color.black_2f3234,R.color.gary_6f777d,0));
                    str=dailyTask.getNoFollowCount()+" ("+
                            dailyTask.getNoFollowRate()+"%)";

                    tvNotVisit.setText(getSpannableString(str,'(',R.color.black_2f3234,R.color.gary_6f777d,0));
                    str=dailyTask.getConvertPotentialCustomerCount()+" ("+
                            dailyTask.getConvertPotentialCustomerRate()+"%)";

                    tvState1.setText(getSpannableString(str,'(',R.color.black_2f3234,R.color.gary_6f777d,0));
                    str=dailyTask.getInvalidCount()+" ("+
                            dailyTask.getInvalidRate()+"%)";
                    tvState2.setText(getSpannableString(str,'(',R.color.black_2f3234,R.color.gary_6f777d,0));
                    tvState3.setText(getSpannableString(dailyTask.getNoHandleCount()+" ("+
                            dailyTask.getNoHandleRate()+"%)",'(',R.color.black_2f3234,R.color.gary_6f777d,0));
                    //颜色
                    int[] colors = new int[2];
                    colors[0] = getResources().getColor(R.color.blue_0f6eff);
                    colors[1]=getResources().getColor(R.color.gray_c0c4cc);
                    int[] colors1=new int[3];
                    colors1[0] = getResources().getColor(R.color.blue_0f6eff);
                    colors1[1]=getResources().getColor(R.color.orange_ffbd30);
                    colors1[2]=getResources().getColor(R.color.gray_c0c4cc);
                    str= dailyTask.getFollowRate()+"%\n已跟进";

                    pieChart1.setCenterText(getSpannableString(str,'%',R.color.blue_0f6eff,R.color.black_2f3234 ,1));
                    str=dailyTask.getConvertPotentialCustomerRate()+"%\n转潜客";
                    pieChart2.setCenterText(getSpannableString(str,'%',R.color.blue_0f6eff,R.color.black_2f3234,1));
                    initPieChart(pieChart1,new int[]{dailyTask.getFollowCount(),dailyTask.getNoFollowCount()},colors);
                    initPieChart(pieChart2,new int[]{dailyTask.getConvertPotentialCustomerCount(),dailyTask.getInvalidCount(),dailyTask.getNoHandleCount()},colors1);

                }
            }
        });

    }

    public SpannableString getSpannableString(String str,char splitChar,int color1,int color2,int offset){
        AbsoluteSizeSpan absoluteSizeSpan12;
        AbsoluteSizeSpan absoluteSizeSpan18;
        ForegroundColorSpan foregroundColorSpanlight;
        ForegroundColorSpan foregroundColorSpanDark;
        StyleSpan styleSpan=new StyleSpan(Typeface.BOLD);

        absoluteSizeSpan12=new AbsoluteSizeSpan(Tool.dip2px(getActivity(),12));
        absoluteSizeSpan18=new AbsoluteSizeSpan(Tool.dip2px(getActivity(),18));
        foregroundColorSpanlight=new ForegroundColorSpan(getResources().getColor(color1));
        foregroundColorSpanDark=new ForegroundColorSpan(getResources().getColor(color2));

        SpannableString spannableString = new SpannableString(str);

        spannableString.setSpan(foregroundColorSpanlight, 0,str.indexOf(splitChar), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(styleSpan,0,str.indexOf(splitChar),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(absoluteSizeSpan18,0,str.indexOf(splitChar),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(foregroundColorSpanDark, str.indexOf(splitChar)+offset,str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(absoluteSizeSpan12,str.indexOf(splitChar)+offset,str.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_daily_task;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        return false;
    }

    public void initPieChart(PieChart pieChart, int[] data,int[]colors) {

        Description description = new Description();
//        description.setText("需要展示的内容");
        description.setEnabled(false);
        pieChart.setDescription(description);
        pieChart.setHoleColor(Color.WHITE);

        pieChart.setCenterTextColor(getResources().getColor(R.color.blue_0f6eff));
        // 设置中心圆孔半径占整个饼状图半径的百分比（100f 是最大=整个图表的半径），默认的50％的百分比（即50f）。

        // 设置中间文字中大小
        pieChart.setCenterTextSize(18f);

        // 百分比显示
        pieChart.setUsePercentValues(true);
        pieChart.setHoleRadius(70f);
        // 禁用触摸
        pieChart.setTouchEnabled(false);
        //设置内置图例不显示
        pieChart.getLegend().setEnabled(false);
        // 设置隐藏饼图上文字，只显示百分比
        pieChart.setDrawSliceText(false);


        //数值
        ArrayList<PieEntry> values = new ArrayList<PieEntry>();
        //数值描述
        ArrayList<String> valuesLabels = new ArrayList<>();


        for (int i = 0; i < data.length; i++) {
            //添加数值
            values.add(new PieEntry(data[i], i));
            //添加数值描述
            PieDataSet pieDataSet = new PieDataSet(values, "");
            pieDataSet.setValueTextColor(Color.WHITE);
            pieDataSet.setValueTextSize(12f);
            //将随机的颜色设置进来
            pieDataSet.setColors(colors);
            PieData pieData = new PieData();
            pieData.setDataSet(pieDataSet);
            //设置百分比显示
            pieData.setDrawValues(false);
            //set data...
            pieChart.setData(pieData);
            pieChart.invalidate();
        }
    }

}
